package server.util;

import server.domain.ServletInfo.ServletInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

/**
 * @Author: hwq
 * @Date: 2020/9/25 20:10
 * @Version 1.0
 */
public class Help {
    public static String toFullyQualifiedName(String shortName, String basePackage) {
        StringBuilder sb = new StringBuilder(basePackage);
        sb.append('.');
        sb.append(StringUtil.trimExtension(shortName));
        //打印出结果
        System.out.println(sb.toString());
        return sb.toString();
    }

    public static List<String> readFromJarFile(String jarPath, String splashedPackageName) throws IOException {

        // System.out.println("从JAR包中读取类: {}"+ jarPath);

        JarInputStream jarIn = new JarInputStream(new FileInputStream(jarPath));
        JarEntry entry = jarIn.getNextJarEntry();
        List<String> nameList = new ArrayList<String>();
        while (null != entry) {
            String name = entry.getName();
            if (name.startsWith(splashedPackageName) && isClassFile(name)) {
                nameList.add(name);
            }

            entry = jarIn.getNextJarEntry();
        }

        return nameList;
    }
    public static String dotToSplash(String name) {
        return name.replaceAll("\\.", "/");
    }
    public static List<ServletInfo> readFromDirectory(String path) {
        File file = new File(path);
        String[] names = file.list();
        List<ServletInfo> names1 = new ArrayList<>();
        if (null == names) {
            return null;
        }
        for (String name : names) {
            names1.add(new ServletInfo(path,path+"/"+name));
        }

        return names1;
    }

    public static boolean isClassFile(String name) {
        return name.endsWith(".class");
    }

    public static boolean isJarFile(String name) {
        return name.endsWith(".jar");
    }
    public static boolean isWebXmlFile(String name) {
        return name.endsWith("web.xml");
    }
    private ClassLoader cl=getClass().getClassLoader();;



    /**
     * 读取class
     *
     * @param filePath
     * @param nameList
     * @return
     * @throws IOException
     */
    public List<ServletInfo> doScan(String filePath, List<ServletInfo> nameList) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {

        List<ServletInfo> names  = readFromDirectory(filePath);

        for (ServletInfo name : names) {
            if (isWebXmlFile(name.getFileName())) {
                nameList.add(new ServletInfo(filePath,name.getFileName()));
            } else {
                if(!name.getFileName().contains(".")){
                    doScan(name.getFileName(),nameList);
                }

            }
        }
        for (ServletInfo n : nameList) {
          System.out.println(n);
        }
        return nameList;
    }
}
